package com.mobi.Survey;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.mobi.Survey.controller.AggregateResultPojo;
import com.mobi.Survey.controller.SurveyPojo;
import com.mobi.Survey.controller.User;
import com.mobi.Survey.model.AbstractTable;
import com.mobi.Survey.model.SurveyResultTable;
import com.mobi.Survey.model.SurveyTable;

/**
 * 
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * SurveyOptionScreen.java This class is responsible for options to user of
 * survey i.e start survey, delete survey see correct answer etc.
 */
public class SurveyOptionScreen extends AbstractScreen
{
	private long surveyId = -1;

	private Button btnStartSurvey;
	private Button btnAnswer;
	private Button btnSurveyResults;

	private SurveyPojo sp;
	AbstractTable at;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{

		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		if (intent != null)
		{
			Bundle bundle = intent.getExtras();
			// survey id should be greater than 0
			if (bundle != null)
			{

				surveyId = ((Long) bundle.get("data")).intValue();

			}
		}

		if (surveyId > -1)
		{
			SurveyTable surveyTable = new SurveyTable(this);
			sp = surveyTable.getSurveys(false, true, this, surveyId).get(0);
			surveyTable.closeDB();
			setTitle(sp.getSurveyDiscription());
			// text for the buttons if we dont have text for a button that
			// button will not be render
			btnStartSurvey.setText(sp.getTxtBtnStartSurvey());
			String strSurveyResult = sp.getTxtBtnSurveyResult();

			if (strSurveyResult != null && strSurveyResult.length() > 0)
			{
				btnSurveyResults.setText(strSurveyResult);
			} else
			{
				btnSurveyResults.setVisibility(View.GONE);
			}
		} else
		{
			finish();
			showShortToast(Messages.INVALID_SURVEY_ID);
		}
		showHeaderImage(User.getInstance().getDetailImage());
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.m_delete:

			showDeleteConfirmationDialogue();

			break;
		}

		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();

		inflater.inflate(R.menu.survey_screen_menu, menu);

		return true;
	}

	private class SurveyDeleteTask extends AsyncTask<String, Object, Object>
	{
		ProgressDialog progressDialog = null;
		private boolean deleted = false;

		protected Object doInBackground(String... params)
		{
			if (surveyId > -1)
			{
				deleted = SurveyPojo.deleteSurvey(sp.getSubscriptionId(),
						surveyId, SurveyOptionScreen.this);
				AggregateResultPojo.delete(SurveyOptionScreen.this, surveyId);
				SurveyListScreen.RE_SYNCH_FROM_DB = true;

			}

			return null;
		}

		protected void onPostExecute(Object result)
		{

			super.onPostExecute(result);
			progressDialog.dismiss();
			progressDialog = null;
			if (deleted)
			{

				finish();
			}

		}

		protected void onPreExecute()
		{
			progressDialog = ProgressDialog.show(SurveyOptionScreen.this, "",
					Messages.DELETING_SURVEY);
			super.onPreExecute();

		}

	}

	/**
	 * shows confirmation dialogue if user really want to delete a survey
	 */
	private void showDeleteConfirmationDialogue()
	{
		final AlertDialog.Builder alert = new AlertDialog.Builder(this);

		alert.setTitle(Messages.ARE_YOU_SURE);
		alert.setMessage("Survey with key " + sp.getSurveyKey()
				+ " will be deleted.");

		alert.setPositiveButton(Messages.YES,
				new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface dialog, int whichButton)
					{
						new SurveyDeleteTask().execute();
					}
				});

		alert.setNegativeButton(Messages.NO,
				new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface dialog, int whichButton)
					{

					}
				});

		alert.show();

	}

	@Override
	protected void onResume()
	{

		super.onResume();

		String strCorrectAnswer = sp.getTxtBtnCorrectAnswer();
		if (((SurveyResultTable) at).isNew(surveyId) > 0
				&& strCorrectAnswer != null && strCorrectAnswer.length() > 0)
		{

			btnAnswer.setText(strCorrectAnswer);
			btnAnswer.setVisibility(View.VISIBLE);
		} else
		{
			btnAnswer.setVisibility(View.GONE);
		}
	}

	@Override
	protected void initUiVars()
	{

		at = new SurveyResultTable(this);
		btnStartSurvey = (Button) findViewById(R.id.btnStartSurvey);
		btnAnswer = (Button) findViewById(R.id.btnAnswer);
		btnSurveyResults = (Button) findViewById(R.id.btnSurveyResults);

		headerImage = findViewById(R.id.headerImage);

	}

	@Override
	protected int getLayoutId()
	{

		return LayoutIds.SURVEY_OPTS_SCREEN;
	}

	@Override
	protected void onRelease()
	{
		at.closeDB();
		sp.getSurveyDetail().clear();
		sp = null;
	}

	public void btnClick(View v)
	{
		if (v.getId() == R.id.btnSurveyInfo)
		{
			onBtnSurveyInfo();

		} else if (v.getId() == R.id.btnStartSurvey)
		{
			onBtnSurveyStart();

		} else if (v.getId() == R.id.btnSurveyResults)
		{
			onBtnSurveyResult();

		} else if (v.getId() == R.id.btnAnswer)
		{
			onBtnAnswer();
		}

	}

	private void onBtnAnswer()
	{
		if (surveyId > 0)
			switchScreenTo(SurveyAnswersScreen.class, Long.valueOf(surveyId));
		else
		{
			showShortToast(Messages.INVALID_SURVEY_ID);
		}

	}

	private void onBtnSurveyResult()
	{
		if (surveyId > 0)
			switchScreenTo(AggregateResultScreen.class, Long.valueOf(surveyId));
		else
		{
			showShortToast(Messages.INVALID_SURVEY_ID);
		}
	}

	private void onBtnSurveyStart()
	{

		if (sp.isShowAllControls()) // Show all controls on single screen
			switchScreenTo(SurveyDetailAllControls.class,
					Long.valueOf(surveyId));
		else
		{
			// show one control/screen
			switchScreenTo(SurveyDetailSingleControl.class,
					Long.valueOf(surveyId));
		}
	}

	private void onBtnSurveyInfo()
	{

		if (surveyId > 0)
			switchScreenTo(SurveyInformation.class, Long.valueOf(surveyId));
		else
		{
			showShortToast(Messages.INVALID_SURVEY_ID);
		}
	}
}
